/*
 * Copyright (c) 2025 Huawei Device, Inc. Ltd. and <马弓手>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "CDVIonicKeyboard.h"
REGISTER_PLUGIN_CLASS(CDVIonicKeyboard)
bool CDVIonicKeyboard::execute(const string&  action, cJSON* args, CallbackContext cbc)
{
    if(action == "hide") {
        m_cbc = cbc;
        executeArkTs("/pages/Keyboard", 0, "", "CDVIonicKeyboard", cbc);
    }
    
    if(action == "show") {
        executeArkTs("/pages/Keyboard", 1, "", "CDVIonicKeyboard", cbc);
    }
    
    if(action == "init") {
        m_cbc2 = cbc;
        executeArkTs("/pages/Keyboard", 2, "", "CDVIonicKeyboard", cbc);
    }
    
    if(action == "onArKTsResult") {
        return onArKTsResult(args);
    }
    
    return true;
}

bool CDVIonicKeyboard::onArKTsResult(cJSON* args)
{
    cJSON* content = cJSON_GetObjectItem(args, "content");
    string action = "";
    if(content != NULL) {
        action = content->valuestring;
    }
    
    if(action == "keyboardHeightChange") {
        cJSON* json = cJSON_GetObjectItem(args, "result");
        if(json != NULL && json->type == cJSON_Array) {
            int count = cJSON_GetArraySize(json);
            if(count > 0) {
                string ret = cJSON_GetArrayItem(json, 0)->valuestring;
                PluginResult pluginResult(PluginResult::OK, ret);
                pluginResult.setKeepCallback(true);
                m_cbc2.sendPluginResult(pluginResult);
            }
        }
    } 
    return true;
}